package javaday04.Homework1MIGONG2;

import java.util.Random;
import java.util.Scanner;

/*
 * 迷宫类
 */
public class Maze {
	private int[][] maze;
	private boolean[][] eventInfo;//记录是否有事件（怪物，药水等）触发
	
	public Maze() {
		super();
		maze=new int[8][8];
		eventInfo=new boolean[8][8];
	}
	/*
	 * 获取迷宫
	 */
	public void getMaze(){
		Random r=new Random();
		maze = new int[8][8];
		for(int i=0;i<maze.length;i++){
			for(int j=0;j<maze[i].length;j++){
				//控制生成障碍概率
				if(r.nextDouble()<=0.2){
					maze[i][j]=1;
					eventInfo[i][j]=true;//表示无事件
			}
		}
		maze[0][0]=maze[7][7]=0;
	}	
	}
	/*
	 * 走迷宫
	 */
	public void walkMaze(Role role){
		Scanner s=new Scanner(System.in);
		//记录当前位置
		int[] now={0,0};
		//记录之前数据
		int[] before={0,0};
		Bag bag=new Bag();
		while (!(now[0]==maze.length-1&&now[1]==maze[now[0]].length-1)){
			printMaze(now);//打印迷宫
			before[0]=now[0];
			before[1]=now[1];
			System.out.println("走w/a/s/d");
			switch(s.next().charAt(0)){
			case 'w':
				if(now[0]>0&&maze[now[0]-1][now[1]]!=1)
					now[0]--;
				break;
			case 'a':
				if(now[1]>0&&maze[now[0]][now[1]-1]!=1)
					now[1]--;
				break;
			case 's':
				if(now[0]<maze.length&&maze[now[0]+1][now[1]]!=1)
					now[0]++;
				break;
			case 'd':
				if(now[1]<maze[now[0]].length&&maze[now[0]][now[1]+1]!=1)
					now[1]++;
				break;
			}
			maze[before[0]][before[1]]=0;
			eventInfo[before[0]][before[1]]=true;//之前走过的位置不能再触发事件
			//触发事件
			if(eventInfo[now[0]][now[1]]==false){
				mazeEvent(role);
			}
			if(role.getBlood()<=0)
				return;
			//查看背包
			System.out.println("查看背包y/n");
			if(s.next().charAt(0)=='y')
					role.getBag().showBag();
		}
		
		
		System.out.println("通关");
	}
	/*
	 * 打印迷宫
	 */
	public void printMaze(int[] now){
		for(int i=0;i<maze.length;i++){
			for(int j=0;j<maze[i].length;j++){
				if(i==now[0]&&j==now[1])
					System.out.print("⭐ ");
				else
					System.out.print(maze[i][j]+"  ");
			}
			System.out.println();
//			System.out.println();
		}
	}
	
	/*
	 * 设置迷宫事件：生成怪物,药水等
	 */
	public void mazeEvent(Role role){
		Random r=new Random();
		
		//生成武器
		if(r.nextDouble()<=0.3){
			Weapon wp=new Weapon("无尽之刃",20);
			System.out.println("捡到武器:"+wp.getName()+",攻击力"+wp.getHurt());
			role.setWp(wp);
			role.getBag().setWp(wp);
		}
		//生成盔甲
		if(r.nextDouble()<=0.3){
			Armour ar=new Armour("荆棘之甲",10);
			System.out.println("捡到盔甲");
			System.out.println("捡到盔甲:"+ar.getName()+",效果:增加"+ar.getProtect()+"生命值");
			//穿戴盔甲
			role.wearArmour(ar);
			role.setAr(ar);
			role.getBag().setAr(ar);
		}
		//生成药水
		if(r.nextDouble()<=0.3){
			Potion pt=new Potion("回复药水",10);
			role.setPt(pt);
			System.out.println("捡到药水:"+pt.getName()+",效果:回复"+pt.getReply()+"生命值");
			//使用药水
			role.usePotion(pt);
			role.getBag().setPt(pt);
		}
		//生成怪物	
		if(r.nextDouble()<=0.3){
			Monster ms=new Monster("深渊巨兽",100,5);
			System.out.println("遇到怪物:"+ms.getName()+",血量"+ms.getBlood()+",攻击力"+ms.getHurt());
			//进行pk
			role.attack(ms);		
		}
	}
}
